package com.lzb.watermarkcamera.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.os.Build;
import com.lzb.watermarkcamera.R;
import android.widget.LinearLayout;

/**
 * 自定义圆角LinearLayout
 * 支持设置四个角的圆角半径
 */
public class RoundedLinearLayout extends LinearLayout {
    
    private Path path;
    private RectF rectF;
    private float[] radii = new float[8]; // 存储四个角的半径
    
    // 圆角半径
    private float topLeftRadius = 0;
    private float topRightRadius = 0;
    private float bottomLeftRadius = 0;
    private float bottomRightRadius = 0;
    
    public RoundedLinearLayout(Context context) {
        super(context);
        init();
    }
    
    public RoundedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        initAttrs(context, attrs);
    }
    
    public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        path = new Path();
        rectF = new RectF();
        setWillNotDraw(false); // 确保onDraw会被调用
        setLayerType(LAYER_TYPE_HARDWARE, null); // 启用硬件加速，提高性能
        
        // API 21+ 使用更高效的outline裁剪
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, android.graphics.Outline outline) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 
                            Math.max(Math.max(topLeftRadius, topRightRadius), 
                                    Math.max(bottomLeftRadius, bottomRightRadius)));
                    }
                }
            });
            setClipToOutline(true);
        }
    }
    
    /**
     * 初始化XML属性
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundedLinearLayout);
        
        try {
            // 获取统一的圆角半径
            float radius = typedArray.getDimension(R.styleable.RoundedLinearLayout_radius, 0);
            if (radius > 0) {
                setRadius(radius);
            } else {
                // 获取单独的圆角半径
                float topLeft = typedArray.getDimension(R.styleable.RoundedLinearLayout_topLeftRadius, 0);
                float topRight = typedArray.getDimension(R.styleable.RoundedLinearLayout_topRightRadius, 0);
                float bottomLeft = typedArray.getDimension(R.styleable.RoundedLinearLayout_bottomLeftRadius, 0);
                float bottomRight = typedArray.getDimension(R.styleable.RoundedLinearLayout_bottomRightRadius, 0);
                
                if (topLeft > 0 || topRight > 0 || bottomLeft > 0 || bottomRight > 0) {
                    setRadius(topLeft, topRight, bottomLeft, bottomRight);
                }
            }
        } finally {
            typedArray.recycle();
        }
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF.set(0, 0, w, h);
        updatePath();
    }
    
    @Override
    public void draw(Canvas canvas) {
        // 保存当前画布状态
        canvas.save();
        // 裁剪画布为圆角矩形
        canvas.clipPath(path);
        // 绘制背景和内容
        super.draw(canvas);
        // 恢复画布状态
        canvas.restore();
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        // 保存当前画布状态
        canvas.save();
        // 裁剪子视图的绘制区域
        canvas.clipPath(path);
        // 绘制子视图
        super.dispatchDraw(canvas);
        // 恢复画布状态
        canvas.restore();
    }
    
    /**
     * 更新圆角路径
     */
    private void updatePath() {
        path.reset();
        
        // 设置四个角的半径
        radii[0] = topLeftRadius;     // 左上角 x
        radii[1] = topLeftRadius;     // 左上角 y
        radii[2] = topRightRadius;    // 右上角 x
        radii[3] = topRightRadius;    // 右上角 y
        radii[4] = bottomRightRadius; // 右下角 x
        radii[5] = bottomRightRadius; // 右下角 y
        radii[6] = bottomLeftRadius;  // 左下角 x
        radii[7] = bottomLeftRadius;  // 左下角 y
        
        // 创建圆角矩形路径
        path.addRoundRect(rectF, radii, Path.Direction.CW);
    }
    
    /**
     * 设置统一的圆角半径
     * @param radius 圆角半径
     */
    public void setRadius(float radius) {
        setRadius(radius, radius, radius, radius);
    }
    
    /**
     * 设置四个角的圆角半径
     * @param topLeft 左上角半径
     * @param topRight 右上角半径
     * @param bottomLeft 左下角半径
     * @param bottomRight 右下角半径
     */
    public void setRadius(float topLeft, float topRight, float bottomLeft, float bottomRight) {
        this.topLeftRadius = topLeft;
        this.topRightRadius = topRight;
        this.bottomLeftRadius = bottomLeft;
        this.bottomRightRadius = bottomRight;
        updatePath();
        invalidate();
        
        // 更新outline
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            invalidateOutline();
        }
    }
    
    /**
     * 设置左上角圆角半径
     */
    public void setTopLeftRadius(float radius) {
        this.topLeftRadius = radius;
        updatePath();
        invalidate();
    }
    
    /**
     * 设置右上角圆角半径
     */
    public void setTopRightRadius(float radius) {
        this.topRightRadius = radius;
        updatePath();
        invalidate();
    }
    
    /**
     * 设置左下角圆角半径
     */
    public void setBottomLeftRadius(float radius) {
        this.bottomLeftRadius = radius;
        updatePath();
        invalidate();
    }
    
    /**
     * 设置右下角圆角半径
     */
    public void setBottomRightRadius(float radius) {
        this.bottomRightRadius = radius;
        updatePath();
        invalidate();
    }
    
    /**
     * 获取左上角圆角半径
     */
    public float getTopLeftRadius() {
        return topLeftRadius;
    }
    
    /**
     * 获取右上角圆角半径
     */
    public float getTopRightRadius() {
        return topRightRadius;
    }
    
    /**
     * 获取左下角圆角半径
     */
    public float getBottomLeftRadius() {
        return bottomLeftRadius;
    }
    
    /**
     * 获取右下角圆角半径
     */
    public float getBottomRightRadius() {
        return bottomRightRadius;
    }
}